package com.gydz.gydzbaseframeworkboot.SystemControl.controller;

import com.gydz.gydzbaseframeworkboot.SystemControl.domain.ShortcutMenu;
import com.gydz.gydzbaseframeworkboot.SystemControl.service.ShortcutMenuService;
import com.gydz.gydzbaseframeworkboot.Common.utils.result.ResultUtils;
import com.gydz.gydzbaseframeworkboot.BaseControl.service.AccountUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 快捷菜单控制层
 * @author caiwf
 */

@RestController
@RequestMapping("/shortcutMenu")
public class ShortcutMenuController {

    @Autowired
    private ShortcutMenuService shortcutMenuService;

    @PostMapping("/list")
    public void list(HttpServletResponse httpServletResponse, @ModelAttribute("shortcutMenu") ShortcutMenu shortcutMenu) throws IOException {
        ResultUtils result =  ResultUtils.succ(200,"快捷菜单查询成功",shortcutMenuService.list(shortcutMenu));
        ResultUtils.responseResult(httpServletResponse,result);
    }

    @GetMapping("/getMenuInfo")
    public ShortcutMenu findByUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AccountUser accountUser = (AccountUser) authentication.getPrincipal();
        Long userId = accountUser.getUserId();
        return shortcutMenuService.findByUserId(userId);
    }

    @PostMapping("/save")
    public void save(HttpServletResponse httpServletResponse, @ModelAttribute("shortcutMenu") ShortcutMenu shortcutMenu) throws IOException {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AccountUser accountUser = (AccountUser) authentication.getPrincipal();
        String username = accountUser.getUsername();
        Long userId = accountUser.getUserId();
        shortcutMenu.setUserId(userId);
        shortcutMenu.setUserName(username);

        shortcutMenuService.save(shortcutMenu);
        ResultUtils result =  ResultUtils.succ("更新成功");
        ResultUtils.responseResult(httpServletResponse,result);
    }

    @DeleteMapping("/{id}")
    public void deleteById(HttpServletResponse httpServletResponse,@PathVariable Long id) throws IOException {
        shortcutMenuService.deleteById(id);
        ResultUtils result =  ResultUtils.succ("");
        ResultUtils.responseResult(httpServletResponse,result);
    }
}
